package com.ctg.behavior.calc.tools;

import com.ctg.behavior.common.calc.CtCalcCondition;
import com.ctg.behavior.common.calc.CtCalcFilter;
import com.ctg.behavior.common.consts.CtFunctionConsts;
import com.ctg.behavior.common.consts.CtRelationConsts;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author Administrator
 * @date 2021/6/4 14:16
 */
public class FunctionToSQLTool {

    /**
     * 转义sql语法
     * @param filter
     * @return
     */
    public String repFunToSql(CtCalcFilter filter){
        StringBuffer condStr = new StringBuffer();
        if(filter != null){
            List<CtCalcCondition> conditions = filter.getConditions();
            if(conditions!= null && conditions.size() > 0){
                int c = 0;
                for(CtCalcCondition conn: conditions){
                    if(conn != null){
                        //多条件
                        if(c > 0 ) {
                            if (CtRelationConsts.KEY_AND.equals(filter.getRelation()) || StringUtils.isEmpty(filter.getRelation())) {
                                condStr.append(" and ");
                            }
                            if (CtRelationConsts.KEY_OR.equals(filter.getRelation())) {
                                condStr.append(" or ");
                            }
                        }
                        /**
                         * equal	等于
                         * notEqual	不等于
                         * less 小于
                         * greater 大于
                         * contain	包含
                         * notContain	不包含
                         * isSet	有值
                         * notSet	没值
                         */
                        if(CtFunctionConsts.KEY_EQUAL.equals(conn.getFunction())){
                            List<String> params = conn.getParams();
                            if(params != null && params.size() > 0) {
                                int i = 0;
                                condStr.append("(");
                                for(String param : params) {
                                    if(i > 0 ) {
                                        condStr.append(" or ");
                                    }
                                    condStr.append(conn.getField()).append(" = '").append(param).append("'");
                                    if(i == params.size() -1){
                                        condStr.append(" )");
                                    }else {
                                        i++;
                                    }
                                }
                            }
                        }
                        if(CtFunctionConsts.KEY_NOTEQUAL.equals(conn.getFunction())){
                            List<String> params = conn.getParams();
                            if(params != null && params.size() > 0) {
                                int i = 0;
                                condStr.append("(");
                                for(@SuppressWarnings("unused") String param : params) {
                                    if(i > 0) {
                                        condStr.append(" or ");
                                    }
                                    condStr.append(conn.getField()).append(" <> '").append(params).append("'");
                                    if(i == params.size() -1){
                                        condStr.append(" )");
                                    }else {
                                        i++;
                                    }
                                }
                            }
                        }
                        if(CtFunctionConsts.KEY_LESS.equals(conn.getFunction())){
                            condStr.append(conn.getField()).append(" < ").append(conn.getParams().get(0));
                        }
                        if(CtFunctionConsts.KEY_GREATER.equals(conn.getFunction())){
                            condStr.append(conn.getField()).append(" > ").append(conn.getParams().get(0));
                        }
                        if(CtFunctionConsts.KEY_CONTAIN.equals(conn.getFunction())){
                            condStr.append(conn.getField()).append(" like '%").append(conn.getParams().get(0)).append("%'");
                        }
                        if(CtFunctionConsts.KEY_NOTCONTAIN.equals(conn.getFunction())){
                            condStr.append(conn.getField()).append(" not like '%").append(conn.getParams().get(0)).append("%'");
                        }
                        if(CtFunctionConsts.KEY_ISSET.equals(conn.getFunction())){
                            condStr.append(conn.getField()).append(" is not null ");
                        }
                        if(CtFunctionConsts.KEY_NOTSET.equals(conn.getFunction())){
                            condStr.append(conn.getField()).append(" is null");
                        }

                        c++;
                    }
                }
            }
            //条件里面包含条件
            List<CtCalcFilter> filters = filter.getFilters();
            if(filters != null && filters.size() > 0){
                String conn1="";
                int f = 0;
                for (CtCalcFilter filter1 : filters){
                    conn1= this.repFunToSql(filter1);
                    if(f > 0  && StringUtils.isNotBlank(conn1)){
                        conn1 = conn1 + filter.getRelation();
                    }
                    if(StringUtils.isNotBlank(conn1)){
                        conn1 = "( "+conn1+" )" ;
                    }
                    f++;
                }
                condStr.append(" ").append(conn1);
            }
        }
        return condStr.toString();
    }

}
